﻿/********
* 
* 
*  Description:
*  Create By kain.hong at 8/10/2010 10:57:32 AM
*  
*
*  Revision History:
*  Date                  Who                 What
*  
* 
*/
#region namespace

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Messaging;
#endregion


namespace Cherry.Agile.ServiceModel
{
    /// <summary>
    /// WCF上下文数据存储槽
    /// </summary>
    [Serializable]
    public class ApplicationContext
    {
        private const string CallContextKey = "__Application_Context";    
        internal const string ContextHeaderLocalName = "__Application_Context";
        internal const string ContextHeaderNamespace = "www.szxys.com/remp";
        Dictionary<string, object> _innerDict = new Dictionary<string, object>();

        private void EnsureSerializable(object value)
        {
            if (value == null)
                throw new ArgumentNullException("value");
            
            if (!value.GetType().IsSerializable)
                throw new ArgumentException(string.Format("类型{0}必须是可序列化的。", value.GetType().FullName));
            
        }

        /// <summary>
        /// 应用程序实例ID
        /// </summary>
        public Guid? InstanceId { get; set; }


        public int ApplicationId { get; set; }

        /// <summary>
        /// 获取和设置存储值
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>和键匹配的值</returns>
        public object this[string key]
        {
            get
            {
                object value;
                _innerDict.TryGetValue(key, out value);
                return value;
            }
            set
            {
                this.EnsureSerializable(value);
                //_innerDict.TryAdd(key, value);
                if (this._innerDict.ContainsKey(key))
                    _innerDict[key] = value;
                else
                    _innerDict.Add(key, value);
            }
        }


        public ApplicationContext() {
            this.ApplicationId = Cherry.Agile.Configuration.AgileConfiguration.Current.ApplicationSection.Id;
        }

        /// <summary>
        /// 语言名称
        /// </summary>
        public string CultureName { get; set; }

        private static object _sync = new object();


        private static System.Threading.ThreadLocal<ApplicationContext> _context = new System.Threading.ThreadLocal<ApplicationContext>();
        /// <summary>
        /// 获取上下<see cref="ApplicationContext"/>
        /// </summary>
        public static ApplicationContext Current
        {
            get
            {
                if (_context.Value == null)
                    _context.Value = new ApplicationContext();
                return _context.Value;
                //if (CallContext.GetData(CallContextKey) == null)
                //{ 
                //    lock(_sync )
                //        if (CallContext.GetData(CallContextKey) == null)
                //            CallContext.SetData(CallContextKey, new ApplicationContext());  
                //} 

                //return CallContext.GetData(CallContextKey) as ApplicationContext;
            }
            internal set
            {
                _context.Value = value;
                //CallContext.SetData(CallContextKey, value);
            }
        }     
    }
} 

